﻿Public Class nueva_bodega
    Dim id As String
    Dim bod_id As String
    Dim qry As String
    Dim flag As Boolean = False
    Dim ceditar As String
    Dim dataset As New DataSet
    Dim dataset2 As New DataSet
    Dim dataset3 As New DataSet
    Public Sub setid(ByVal id As Integer, ByVal ceditar As String)
        Me.id = id
        Me.ceditar = ceditar
        flag = True
    End Sub
    Private Sub nueva_bodega_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Icon = (My.Resources.favicon)
        'CargarCombobox("select usr_id,(usr_nombre + ' ' +usr_apellido)as nombre from usuarios", ComboBox1, "usr_id", "nombre")
        If (flag) Then
            Me.Text = "Editar Bodega"
            subtitulo.Text = "Editar Bodega"
            btn_aceptar.Hide()
            btn_editar.Show()
            'carga
            ceditar = "select bod_nombre,bod_descripcion from bodegas"
            ceditar &= "  where bod_id='" & id & "'"
            Consulta(ceditar, dataset)
            TextBox1.Text = dataset.Tables("data").Rows(0)(0)
            RichTextBox1.Text = dataset.Tables("data").Rows(0)(1)


            'carga listbox
            Consulta("select usuarios.usr_id,(usr_nombre + ' ' + usr_apellido) as nombre from usuarios inner join usuario_bodega on usuarios.usr_id=usuario_bodega.usr_id where bod_id='" & id & "'", dataset2)
            qry = "select usuarios.usr_id,(usr_nombre + ' ' + usr_apellido) as nombre from usuarios where USR_ID<>'-1'"
            For Each usr_id In dataset2.Tables("data").Rows
                qry &= " and usr_id<>'" & usr_id(0) & "'"
            Next
            Consulta(qry, dataset)
            ListBox1.DisplayMember = "nombre"
            ListBox1.ValueMember = "usr_id"
            ListBox2.DisplayMember = "nombre"
            ListBox2.ValueMember = "usr_id"
            ListBox1.DataSource = dataset.Tables("data")
            ListBox2.DataSource = dataset2.Tables("data")
        Else
            Consulta("select usr_id,(usr_nombre + ' ' + usr_apellido) as nombre from usuarios", dataset)
            Consulta("select usr_id,(usr_nombre + ' ' + usr_apellido) as nombre from usuarios where usr_id=-1", dataset2)
            ListBox1.DisplayMember = "nombre"
            ListBox1.ValueMember = "usr_id"
            ListBox2.DisplayMember = "nombre"
            ListBox2.ValueMember = "usr_id"
            ListBox1.DataSource = dataset.Tables("data")
            ListBox2.DataSource = dataset2.Tables("data")
            btn_editar.Hide()
        End If


            End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub btn_aceptar_Click(sender As System.Object, e As System.EventArgs) Handles btn_aceptar.Click
        If ValidarVacio(Me, Me.ErrorProvider1) = True Then
            qry = "insert into bodegas values('" & TextBox1.Text & "','" & RichTextBox1.Text & "')"
            qry = qry & " ;SELECT @@Identity AS Indentity"
            Consulta(qry, dataset3)
            bod_id = dataset3.Tables("data").Rows(0)(0)
            insertaResponsables(bod_id)
            'registro movimiento
            registro("agregar", login.usuario.id, main.modulo.bodega)
            Me.Close()
            man_bodegas.actualizar()
        End If
    End Sub
    Public Sub insertaResponsables(ByVal bod_id As String)
        For Each usr_id In dataset2.Tables("data").Rows
            qry = "insert into usuario_bodega values ('" & usr_id(0) & "','" & bod_id & "')"
            ' MsgBox(qry)
            EjecutaConsulta(qry)
        Next

    End Sub
    Public Sub actualizaResponsables(ByVal bod_id As String)
        qry = "delete from usuario_bodega where bod_id='" & bod_id & "'"
        EjecutaConsulta(qry)
        For Each usr_id In dataset2.Tables("data").Rows
            qry = "insert into usuario_bodega values ('" & usr_id(0) & "','" & bod_id & "')"
            ' MsgBox(qry)
            EjecutaConsulta(qry)
        Next
    End Sub

    Private Sub btn_editar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_editar.Click
        If ValidarVacio(Me, Me.ErrorProvider1) = True Then
            qry = "update bodegas set bod_nombre='" & TextBox1.Text & "', bod_descripcion='" & RichTextBox1.Text & "' where bod_id='" & id & "'"
            EjecutaConsulta(qry)
            actualizaResponsables(id)
            'registro movimiento
            registro("modificar", login.usuario.id, main.modulo.bodega)
            Me.Close()
            man_bodegas.actualizar()

        End If
    End Sub

    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        ValidarCaracteresEspeciales(e)

    End Sub

    Private Sub RichTextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Dim it As String = ListBox1.SelectedValue
        'dataset.Tables("data").DefaultView.RowFilter = "usr_id=" & it
        'ListBox2.DataSource = dataset.Tables("data").DefaultView
        'verrr!!!!!
        If ListBox1.Items.Count > 0 Then
            dataset2.Tables("data").Rows.Add(ListBox1.SelectedItem(0), ListBox1.SelectedItem(1))
            dataset.Tables("data").Rows.RemoveAt(ListBox1.SelectedIndex)
        End If


    End Sub
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        If ListBox2.Items.Count > 0 Then
            dataset.Tables("data").Rows.Add(ListBox2.SelectedItem(0), ListBox2.SelectedItem(1))
            dataset2.Tables("data").Rows.RemoveAt(ListBox2.SelectedIndex)
        End If
    End Sub
End Class